package com.bytech.common.utils;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author zhao
 * @Create 2021/7/22 17:28
 */
@Slf4j
public class StringUtils {

    private final static String UNDERLINE = "_";

    /**
     * 驼峰转下划线
     */
    public static String humpToUnderline(String para) {
        if (isBlank(para)) {
            return para;
        }
        StringBuilder sb = new StringBuilder(para);
        int temp = 0;//定位
        if (!para.contains(UNDERLINE)) {
            for (int i = 0; i < para.length(); i++) {
                if (Character.isUpperCase(para.charAt(i))) {
                    sb.insert(i + temp, UNDERLINE);
                    temp += 1;
                }
            }
        }
        return sb.toString().toLowerCase();
    }

    /**
     * 判断是否空白
     */
    public static boolean isBlank(final String str) {
        int strLen;
        if ((str == null) || ((strLen = str.length()) == 0)) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (!Character.isWhitespace(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断是否空白
     */
    public static boolean isNotBlank(final String str) {
        return !isBlank(str);
    }

    /**
     * 将字符串装换为MD5
     */
    public static String strToMd5(String str) {
        String md5Str = null;
        if (str != null && str.length() != 0) {
            try {
                MessageDigest md = MessageDigest.getInstance("MD5");
                md.update(str.getBytes());
                byte b[] = md.digest();

                int i;
                StringBuffer buf = new StringBuffer("");
                for (int offset = 0; offset < b.length; offset++) {
                    i = b[offset];
                    if (i < 0) {
                        i += 256;
                    }
                    if (i < 16) {
                        buf.append("0");
                    }
                    buf.append(Integer.toHexString(i));
                }
                //32位
                md5Str = buf.toString();
                //16位
                //md5Str = buf.toString().substring(8, 24);
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
        }
        return md5Str;
    }

    /**
     * 返回随机32位MD5串
     */
    public static String getUUID32() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * 通过正则提取 target 字符串中 以 begin 开头 以 end 结尾的第一个命中内容
     */
    public static String getStrBetween(String begin, String end, String target) {
        Pattern p = Pattern.compile("(?<=" + begin + ").*?(?=" + end + ")");
        Matcher m = p.matcher(target);
        String result = "";
        while (m.find()) {
            result = m.group();
            break;
        }
        return result;
    }

    /**
     * 通过正则提取 target 字符串中 以 begin 开头 以 end 结尾的所有内容
     */
    public static List<String> getStrsBetween(String begin, String end, String target) {
        Pattern p = Pattern.compile("(?<=" + begin + ").*?(?=" + end + ")");
        Matcher m = p.matcher(target);
        List<String> resultList = Lists.newArrayList();
        while (m.find()) {
            resultList.add(m.group());
        }
        return resultList;
    }

    /**
     * 字符串转base64
     */
    public static String base64Encode(String str) {
        byte[] encodedBytes = Base64.getEncoder().encode(str.getBytes());
        return new String(encodedBytes, StandardCharsets.UTF_8);
    }

    /**
     * base64转字符串
     */
    public static String base64Decode(String base64Str) {
        byte[] decodedBytes = Base64.getDecoder().decode(base64Str.getBytes());
        return new String(decodedBytes, StandardCharsets.UTF_8);
    }

    /**
     * 字符串转base64 带编码版本
     */
    public static String base64Encode(String str, String charset) {
        byte[] encodedBytes = Base64.getEncoder().encode(str.getBytes());
        return new String(encodedBytes, Charset.forName(charset));
    }

    /**
     * base64转字符串 带编码版本
     */
    public static String base64Decode(String base64Str, String charset) {
        byte[] decodedBytes = Base64.getDecoder().decode(base64Str.getBytes());
        return new String(decodedBytes, Charset.forName(charset));
    }

    /**
     * 邮箱校验
     */
    public static boolean isEmailLegal(String email) {
        if (isBlank(email)) {
            return false;
        }
        return email.matches("^([a-zA-Z0-9]+[_|\\_|\\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\\_|\\.]?)*[a-zA-Z0-9]+\\.[a-zA-Z]{2,3}$");
    }
}
